//
//  SXStatusPictureView.swift
//  sina
//
//  Created by shengxinwei on 16/5/25.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//图片之间的间距
private let pictureMargin: CGFloat = 3
//最大宽度
private let maxWidth = ScreenWidth - 2 * cellMargin
//单张图片的宽度
private let itemWidth = (maxWidth - 2 * pictureMargin) / 3

private let pictureCellId = "pictureCellId"
class SXStatusPictureView: UICollectionView {

    var pictures: [SXPicture]? {
        didSet {
            testLabel.text = "\(pictures?.count ?? 0)"
            let collectionSize = calculatePictureViewSize()
            self.snp_updateConstraints { (make) in
                make.size.equalTo(collectionSize)
            }
            reloadData()
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = pictureMargin
        layout.minimumInteritemSpacing = pictureMargin
        super.init(frame: frame, collectionViewLayout: layout)
        //设置数据源代理
        self.dataSource = self
        //设置点击状态,,自动滚到顶部,,
        scrollsToTop = false
        setupUI()
        //注册cell
        registerClass(SXPictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        backgroundColor = UIColor.whiteColor()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
    }
    
    func calculatePictureViewSize() -> CGSize {
        let imageCount = pictures?.count ?? 0
        if imageCount == 0{
            return CGSizeZero
        }
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        if imageCount == 1 {
            //按照原图原比例大小显示
            let imageSize = CGSizeMake(100, 150)
            //还需要设置itemSize 和 图片一样大
            layout.itemSize = imageSize
            return imageSize
        }
        
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        if imageCount == 4 {
            let w = itemWidth * 2 + pictureMargin
            return CGSizeMake(w, w)
        }
        
        let row = CGFloat((imageCount-1) / 3) + 1
        let maxHeight = row * itemWidth + (row - 1) * pictureMargin
        return CGSizeMake(maxWidth, maxHeight)
    }
    
    
    private lazy var testLabel: UILabel = UILabel(title: "", font: 30, textColor: UIColor.redColor())
    
    
    
   
    
}

extension SXStatusPictureView : UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! SXPictureCell
        cell.picture = self.pictures![indexPath.item]
        
        return cell
    }

    
}


class SXPictureCell: UICollectionViewCell {
    
    //拥有模型属性
    var picture: SXPicture? {
        didSet{
            let url = NSURL.init(string: picture?.thumbnail_pic ?? "")
            picView.sd_setImageWithURL(url)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupCellUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupCellUI() {
        contentView.addSubview(picView)
        
        picView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
        
    }
    
    
    lazy var picView: UIImageView = {
       let img = UIImageView()
        //设置图片填充模式
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
    
    
    
    
    
}
